package com.lzy.usermanager.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.abel533.entity.Example;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzy.usermanager.bean.EasyUIResult;
import com.lzy.usermanager.mapper.UserMapper;
import com.lzy.usermanager.pojo.User;
import com.lzy.usermanager.service.UserService;

@Service
public class UserServiceImpl implements UserService {
    
    @Autowired
    private UserMapper userMapper;
    
    @Override
    public EasyUIResult queryUserListEasyUIResult(Integer page, Integer rows) {
        
        //设置分页参数
        PageHelper.startPage(page, rows);
        
        //查询User数据
//        List<User> userList = userMapper.select(null);
        //设置排序条件查询
        Example example = new Example(User.class);
        example.setOrderByClause("updated DESC");//设置排序条件:根据“更新时间”降序
        List<User> userList = userMapper.selectByExample(example);
        
        PageInfo<User> pageInfo = new PageInfo<User>(userList);
        
        return new EasyUIResult(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 根据id查询用户User
     */
    @Override
    public User queryUserById(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增用户User
     */
    @Override
    public Boolean saveUser(User user) {
        int insert = userMapper.insert(user);
        return insert==1;
    }

    /**
     * 更新
     */
    @Override
    public Boolean updateUser(User user) {
        return userMapper.updateByPrimaryKeySelective(user)==1;
    }

    /**
     * 根据id删除用户User
     */
    @Override
    public Boolean deleteUser(Long id) {
        return userMapper.deleteByPrimaryKey(id)==1;
    }

}
